/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

ShpTool = function()
{
	this.setImg = function(idForm)
	{
		var formImg = $(idForm)[0];
		formImg.method = "post";
		formImg.enctype="multipart/form-data";
		formImg.action= sPortletJSP + "upload/uploadQT.jsp?server=" + serverName + "&portlet_jsp=" + sPortletJSP;
	};
	
	this.importSHP = function(idForm, idDivPreview) {
		disableMainPage();
    	var sPortlet = sPortletImg + "loader.gif";
		var divPreview = $(idDivPreview)[0];
		var formImg = $(idForm)[0];
        $(divPreview).html('');
        $(divPreview).html('<img src="' + sPortlet + '" alt="Uploading...."//>');
        $(divPreview).hide();
        $(formImg).ajaxForm({
            target: idDivPreview,
            success: function(msg){
            	msg = base.trim(msg.replace(/(\r\n|\n|\r)/gm, ""));
            	$(idDivPreview).text(msg);
                enableMainPage();
                if(msg == "1") alert(LANGUAGE['import_shp_success']);
                else alert(LANGUAGE['import_shp_failed']);
	          },
	          error: function(XMLHttpRequest, textStatus, errorThrown) {
	        	  alert(LANGUAGE['import_shp_failed']);
                 enableMainPage();
	          }
        }).submit();
	};
	
    this.loadSHP = function()
    {
        $.ajax({
            type: "POST",
            url: "/AnGiang-557-portlet/html/giamsatmoitruong/SHP/loadSHP.jsp",
            data: "dataString",
            success: function (data) {
                var result = base.trim(data.replace(/(\r\n|\n|\r)/gm, ""));        	
                if(result == "0")
                {
                    alert("0");
                    return;
                }
                $('#divPreview_Map_BandoquyhoachChung_ImageUpload_Action').empty();
                $('#divPreview_Map_BandoquyhoachChitiet_ImageUpload_Action').empty();
            	var indexLayer = LayerTool.getIndexLayer();
            	var layerObj = map.layers[indexLayer];
                var arrFeatures = result.split('&');
                var i=0, j=0, k=0;
                for(k=0;k<arrFeatures.length-1;k++)
                {
    	           // create some attributes for the feature
    	           var attributes = new Object();
                	          
                    var arrItems = arrFeatures[k].split('|');
                    var arrAttrs = arrItems[0];
                    var arrCoors = arrItems[1];
                    // lay data
                    var _arrAttrs = arrAttrs.split(';');
                    for(i=0;i<_arrAttrs.length-1;i++)
                    {
                        var __arrAtts = _arrAttrs[i].split(':');
                        // ten truong
                        var sFieldName = __arrAtts[0];
                        // du lieu
                        var sData = __arrAtts[1];
                        attributes[sFieldName] = sData;
                    }
                    // lay coor

                    var point = null;
                	var points = [];
    	           
                    var _arrCoors = arrCoors.split(';');
                    for(j=0;j<_arrCoors.length-1;j++)
                    {
                        var __arrCoors = _arrCoors[j].split(',');
                        var x = __arrCoors[0];
                        var y = __arrCoors[1];
                        point = new OpenLayers.Geometry.Point(x, y);
                        points[points.length] = point;
                    }
    	           var ring = new OpenLayers.Geometry.LinearRing(points);
    	           var polygon = new OpenLayers.Geometry.Polygon([ring]);
    	           var multyPolygon = new OpenLayers.Geometry.MultiPolygon(polygon);
     	           var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.MultiPolygon(), attributes);
     	           feature.state = OpenLayers.State.INSERT;
     	           layerObj.addFeatures([feature]);
                }
                layerObj.redraw(true);
                return;
            }
        });
    };
};
var shptool = new ShpTool();